﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTSPConst;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthEntity;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using JWBSTAPP.CommonClass;
namespace JWBSTAPP.AjaxServer
{ 
    /// <summary>
    /// EntIndexLeftMenu 的摘要说明
    /// </summary>
    public class EntIndexLeftMenu : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public class TreeNode
        {
            public List<TreeNode> children = new List<TreeNode>();//多级
            public string url { get; set; }//连接
            public string name { get; set; }//菜单名称
            public string image { get; set; }//图标
            public bool isAbsolutePath { get; set; } //是否绝对路径
            public bool isOtherArea { get; set; } //是否是本领域链接
            public bool isHref { get; set; } //点击是否弹出链接
        }
        private string getAuthBySmallImage()
        {
            HttpContext context = HttpContext.Current;
            string json = string.Empty;

            List<TreeNode> TreeNodes = new List<TreeNode>();
            //Global.AreaID
            XWG.Framework.IAuthority.IAuth auth = new XWG.Framework.IAuthority.Authority();
            DataTable dtParent = auth.SelectUserResImagePermission("S", Convert.ToString(context.Session["UserID"]));

            DataRow[] drs = dtParent.Select(AppResouRceConst.Columns.ResPid + "='0'");
            foreach (DataRow dr in drs)
            {
                TreeNode node = new TreeNode();
                node.isOtherArea = false;
                node.isAbsolutePath = false;
                node.isHref = false;
                node.children = GetChildNodes(dtParent, Convert.ToString(dr[AppResouRceConst.Columns.ResId]));
                if (Convert.ToString(dr[AppResouRceConst.Columns.ResPathtyPe]) == "0") //相对路径
                {
                    if (Convert.ToString(dr[AppResouRceConst.Columns.AreaId]) == Global.AreaID)
                    {
                        node.url = Convert.ToString(dr[AppResouRceConst.Columns.ResPvalue]);
                    }
                    else
                    {
                        node.url = Convert.ToString(dr[AppAreAConst.Columns.AreaDomain]) + "/" + Convert.ToString(dr[AppResouRceConst.Columns.ResPvalue]);
                        node.isOtherArea = true;
                    }
                }
                else
                {
                    node.isAbsolutePath = true;
                    node.url = Convert.ToString(dr[AppResouRceConst.Columns.ResPvalue]).Contains("SSO") ? Convert.ToString(dr[AppResouRceConst.Columns.ResPvalue]) + "&Token=" + Convert.ToString(context.Session["Token"]) : Convert.ToString(dr[AppResouRceConst.Columns.ResPvalue]);
                }
                node.name = Convert.ToString(dr[AppResouRceConst.Columns.ResName]);
                node.image = Convert.ToString(dr[AppImageConst.Columns.ImageValue]);

                TreeNodes.Add(node);
            }
            for (int i = 0; i < dtParent.Rows.Count; i++)
            {

            }
            json = JsonConvert.SerializeObject(TreeNodes);
            return json;
        }
        private List<TreeNode> GetChildNodes(DataTable dt, string id)
        {

            List<TreeNode> nodes = new List<TreeNode>();

            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPid]) == id)
                        {
                            TreeNode node = new TreeNode();
                            node.isOtherArea = false;
                            node.isAbsolutePath = false;
                            node.isHref = false;
                            node.children = GetChildNodes(dt, Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResId]));
                            if (node.children == null || node.children.Count == 0)
                            {
                                node.isHref = true;
                            }
                            if (Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPathtyPe]) == "0") //相对路径
                            {


                                if (Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.AreaId]) == Global.AreaID)
                                {
                                    node.url = Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPvalue]);
                                }
                                else
                                {
                                    node.url = Convert.ToString(dt.Rows[i][AppAreAConst.Columns.AreaDomain]) + "/";
                                    node.url += Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPvalue]).Contains("SSO") ? Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPvalue]) + "&Token=" + Convert.ToString(HttpContext.Current.Session["Token"]) : Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPvalue]);
                                    node.isOtherArea = true;
                                }
                            }
                            else
                            {
                                node.url = Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPvalue]).Contains("SSO") ? Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPvalue]) + "&Token=" + Convert.ToString(HttpContext.Current.Session["Token"]) : Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPvalue]);
                            }
                            if (Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResPathtyPe]) == "1")
                                node.isAbsolutePath = true;
                            node.name = Convert.ToString(dt.Rows[i][AppResouRceConst.Columns.ResName]);
                            node.image = Convert.ToString(dt.Rows[i][AppImageConst.Columns.ImageValue]);

                            nodes.Add(node);
                        }
                    }
                }

                return nodes;
            }
            catch (Exception ex)
            {

                return null;
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string role = context.Request.Params["userRole"];
            string jsonDataString = getAuthBySmallImage();
            context.Response.Write(jsonDataString);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}